package com.wgl.express.UI;

import com.wgl.express.entity.Station;
import com.wgl.express.entity.User;
import com.wgl.express.service.Impl.StationServiceImpl;
import com.wgl.express.service.Impl.UserServiceImpl;
import com.wgl.express.service.StationService;
import com.wgl.express.service.UserService;
import com.wgl.express.util.Util;

import java.util.*;
import java.util.stream.Collectors;

import static com.wgl.express.text.Text.*;

public class Admin {
    private static Util util;
    Map<Station, Integer> stationPack;
    List<Station> stations;

    public void start() {
        util = Util.getUtil();
        loadPackInfos();
        eventLoop();
    }

    private void loadPackInfos() {
        StationService stationService = new StationServiceImpl();
        List<Station> countOfStation = stationService.getCountOfStation();
        stationPack = new HashMap<>();
        countOfStation.forEach((s) -> stationPack.put(s, 0));
        countOfStation.forEach(
                station -> {
                    Optional<Integer> integer = station.getPosts().stream().map(User::getCount).reduce(Integer::sum);
                    int i = integer.orElse(0);
                    stationPack.put(station, stationPack.get(station) + i);
                });
    }

    private void eventLoop() {
        boolean exit = false;
        int choose;

        while (!exit) {
            util.say(TITLE);
            util.say(MENU_COUNT);

            stationPack.forEach(Admin::showInfo);

            util.say(MENU_ADMIN);
//            util.readLine();
            if ((choose = util.readInt()) == -1) continue;
            switch (choose) {
                case 0:
                    exit = true;
                    break;
                case 1:
                    manageStation();
                    break;
                case 2:
                    managePost();
                    break;

            }
        }
    }

    private void managePost() {
        int sid, choose;
        boolean exit = false;
        do {
            util.say(LINE);
            util.say("===新建快递员信息===\n");
            User post  = new User();
            String name, phone, password;
            util.readLine();
            util.say("请输入快递员名称：");
            if (!(name = util.readLine()).equals("")) post.setUsername(name);
            util.say("请输入快递员电话：");
            if (!(phone = util.readLine()).equals("")) post.setPhone(phone);
            util.say("请输入密码：");
            if (!(password = util.readLine()).equals("")) post.setPassword(password);
            util.say("===确认信息===\n");
            showPost(post);
            util.say("确认提交？\n1.\t确认\t0.\t取消\n");
            // 重试
            if ((choose = util.readInt()) == -1) continue;

            // 如果提交
            if (choose == 1) {
                UserService userService = new UserServiceImpl();
                post.setType(3);
                int i = userService.register(post);
                if (i == 1) util.say("新建成功！\n");
                else util.say("新建失败！\n");
            }

            exit = true;

        } while (!exit);
    }

    private void showPost(User post) {
        if (post.getUid() !=0) util.say("快递员编号：\t" + post.getUid() + "\n");
        util.say("快递员姓名：\t" + post.getUsername() + "\n");
        util.say("快递员电话：\t" + post.getPhone() + "\n");
        util.say("快递员密码：\t" + post.getPassword() + "\n");
        util.say(LINE);
    }

    private void manageStation() {
        boolean exit = false;
        int choose;


        StationService stationService = new StationServiceImpl();
        stations = stationService.selectAll();

        while (!exit) {
            util.say(LINE);
            util.say(PACK_LIST);

            stations.forEach(Admin::showStation);
            util.say(MENU_STATION_MANAGE);

            if ((choose = util.readInt()) == -1) continue;
            switch (choose) {
                case 0:
                    exit = true;
                    break;
                case 1:
                    newStation();
                    break;
                case 2:
                    updateStation();
                    break;

            }
        }
    }

    private void newStation() {
        int sid, choose;
        boolean exit = false;
        do {
            util.say(LINE);
            util.say("===新建站点信息===\n");
            Station s = new Station();
            String name, address, capacity;
            util.readLine();
            util.say("请输入站点名称：");
            if (!(name = util.readLine()).equals("")) s.setStationname(name);
            util.say("请输入站点地址：");
            if (!(address = util.readLine()).equals("")) s.setStationaddress(address);
            util.say("请输入站点容量：");
            if (!(capacity = util.readLine()).equals("")) s.setCapacity(Integer.parseInt(capacity));
            util.say("===确认信息===\n");
            showStation(s);
            util.say("确认提交？\n1.\t确认\t0.\t取消\n");
            // 重试
            if ((choose = util.readInt()) == -1) continue;

            // 如果提交
            if (choose == 1) {
                StationService stationService = new StationServiceImpl();
                int i = stationService.addOne(s);
                if (i == 1) util.say("新建成功！\n");
                else util.say("新建失败！\n");
            }

            exit = true;

        } while (!exit);
    }

    private void updateStation() {
        int  choose;
        boolean exit = false;
        do {
            util.say("请输入站点编号：");
            int sid = util.readInt();
            List<Station> collect = stations.stream().filter((s) -> s.getSid() == sid).collect(Collectors.toList());
            if (collect.size() == 1) {
                util.say(LINE);

                Station s = collect.get(0);
                String name, address, capacity;
                util.readLine();

                util.say("请输入站点名称：");
                if (!(name = util.readLine()).equals("")) s.setStationname(name);
                util.say("请输入站点地址：");
                if (!(address = util.readLine()).equals("")) s.setStationaddress(address);
                util.say("请输入站点容量：");
                if (!(capacity = util.readLine()).equals("")) s.setCapacity(Integer.parseInt(capacity));
                util.say("===确认信息===\n");
                showStation(s);
                util.say("确认提交？\n1.\t确认\t0.\t取消\n");
                if ((choose = util.readInt()) == -1) continue;
                if (choose == 1) {
                    StationService stationService = new StationServiceImpl();
                    int i = stationService.changeOne(s);
                    if (i == 1) util.say("修改成功！\n");
                    else util.say("修改失败！\n");
                }
                exit = true;

            } else {
                util.say("站点编号错误！\n");
            }
        } while (!exit);

    }


    private static void showStation(Station station) {
        if (station.getSid() !=0) util.say("站点编号：\t" + station.getSid() + "\n");
        util.say("站点名称：\t" + station.getStationname() + "\n");
        util.say("站点地址：\t" + station.getStationaddress() + "\n");
        util.say("站点容量：\t" + station.getCapacity() + "\n");
        util.say(LINE);
    }

    private static void showInfo(Station station, Integer integer) {
        util.say("站点编号：\t" + station.getSid() + "\n");
        util.say("站点名称：\t" + station.getStationname() + "\n");
        util.say("站点地址：\t" + station.getStationaddress() + "\n");
        util.say("站点容量：\t" + station.getCapacity() + "\n");
        util.say("站内包裹数量：\t" + integer + "\n");
        util.say(LINE);
    }

}
